<template>
	<view class="payment">
		<view class="card head u-flex u-flex-between" v-if="detailsobj.id">
			<view>
				{{detailsobj.is_grab==1?'极速问诊--':''}}
				<block v-if="type=='report'">报告解读</block>
				<block v-else-if="type=='tw'">图文问诊</block>
				<block v-else-if="type=='sp'">视频问诊</block>
				<block v-else-if="type=='qy'">签约</block>
			</view>
			<view class="money">￥{{detailsobj.total_money}}</view>
		</view>
		<view class="card">
			<view class="inner">
				<view class="item u-flex" v-if="detailsobj.user_the_patient">
					<view class="label">患者信息：</view>
					<view class="u-flex u-flex-y-center">
						<view class="mr-30">{{detailsobj.user_the_patient.name}}</view>
						<view class="mr-30">{{detailsobj.user_the_patient.sex==1?'男':'女'}}</view>
						<view>{{detailsobj.user_the_patient.mobile}}</view>
					</view>
				</view>
				<view class="item u-flex" v-if="type!='report'">
					<view class="label">咨询目的：</view>
					<view>{{detailsobj.consultation_consult_name}}</view>
				</view>
				<view class="item u-flex">
					<view class="label">病情描述：</view>
					<view>{{type=='report'?'报告解读':detailsobj.content}}</view>
				</view>
				<view class="item u-flex">
					<view class="label">患外照片：</view>
					<view class="imglist">
						<view v-if="!detailsobj.images || detailsobj.images.length<=0">暂无</view>
						<image :src="img" mode="aspectFill" v-for="(img,index) in detailsobj.images" :key="index"></image>
					</view>
				</view>
			</view>
			<view class="coupon u-flex u-flex-between" v-if="type=='tw'||type=='sp'||type=='report'">
				<view>优惠券</view>
				<view>
					<view class="u-flex" v-if="couponobj.id">
						<view class="u-flex" style="margin-right: 10rpx;" @click="gotoSelectCoupon">
							<view>{{couponobj.title}}</view>
							<view class="price">-￥{{couponobj.value}}</view>
						</view>
						<u-icon name="close" @click="unCoupon"></u-icon>
					</view>
					<view class="u-flex" v-else @click="gotoSelectCoupon">
						<view>{{((consultation_money||0)<=0||couponnum<=0)?'暂无可用':'选择'}}</view>
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
			</view>
			<view class="hjprice u-flex u-flex-end">
				<view>合计：</view>
				<view class="price">￥{{totalPrice}}</view>
			</view>
		</view>

		<!-- 立即支付 -->
		<view class="qx_button">
			<view @click="Onpaywait" class="btn-fillet">立即支付</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				beforeRoute: '',
				
				id: null,
				type: '', //类型
				detailsobj: {}, //数据
				countdown: '15:00',
				
				couponobj: {}, //选中优惠券
				couponnum: 0, //可用优惠券数量
				
				consultation_money: 0,
				totalPrice: 0,
			}
		},
		onLoad: function(options) {
			this.id = options.id
			this.type = options.type
			
			var pages = getCurrentPages(); // 当前页面
			if(pages.length>=2){
				var beforePage = pages[pages.length - 2]; // 前一个页面
				this.beforeRoute = beforePage.route;
				// console.log('beforePage1',this.beforePage,pages)
			}else{
				// console.log('beforePage2',this.beforePage,pages)
			}
			
			// #ifdef MP-WEIXIN
			wx.enableAlertBeforeUnload({
				message: "是否放弃本次支付？",
				success: function (res) { console.log("方法注册成功：", res);},
				fail: function (errMsg) { console.log("方法注册失败：", errMsg);},
			});
			// #endif
			
			this.$store.commit('selectCouponobj', {})
			
			//详情图文
			this.Onorderdetails()
			this.Oncountdown()
		},
		onShow(){
			console.log('onShow',this.$store.state.couponobj)
			this.couponobj = this.$store.state.couponobj
			this.computedTotalPrice()
		},
		onUnload(){
			this.$store.commit('selectCouponobj', {})
		},
		onBackPress(e) {
			var that = this;
			if (e.from == "backbutton") {
				uni.showModal({
					// title: '提示',
					content: '是否放弃本次支付？',
					cancelText: '放弃',
					confirmText: '继续支付',
					success: (res) => {
						if (res.cancel) {
							// uni.navigateBack({
							// 	delta: 1
							// });
							var url = '';
							if(that.type=='tw'){ //图文问诊
								if(!that.beforeRoute || that.beforeRoute!='pagesA/my/textList/textList') url = '/pagesA/my/textList/textList';
							}else if(that.type=='sp'){ //视频问诊
								if(!that.beforeRoute || that.beforeRoute!='pagesA/my/videoList/videoList') url = '/pagesA/my/videoList/videoList';
							}else if(that.type=='report'){ //报告解读
								if(!that.beforeRoute || that.beforeRoute!='pagesL/my/reportpage') url = '/pagesL/my/reportpage';
							}else if(that.type=='qy'){ //签约
								if(!that.beforeRoute || that.beforeRoute!='pagesL/my/signpage') url = '/pagesL/my/signpage';
							}
							
							if(url){
								uni.redirectTo({
									url: url
								})
							}else{
								uni.navigateBack({
									delta: 1
								});
							}
						}
					}
				});
				return true; //阻止默认返回行为
			}
		},
		methods: {
			computedTotalPrice(){
				this.consultation_money = this.detailsobj.total_money || 0
				if(this.couponobj.least && this.couponobj.least>this.consultation_money){
					this.$store.commit('selectCouponobj', {})
					this.couponobj = {}
				}
				this.$Http({
					url: 'getCouponSelectCount',
					data: {
						type: 1,
						price: this.consultation_money,
					},
					load: false,
				}).then(res=>{
					if(res.code == 1) this.couponnum = res.data;
				});

				var totalPrice = parseFloat(this.consultation_money);
				if(this.couponobj.id) totalPrice -= parseFloat(this.couponobj.value || 0);
				if(totalPrice<0) totalPrice = 0;

				this.totalPrice = totalPrice.toFixed(2)
			},
			
			// 取消优惠券
			unCoupon(){
				this.$store.commit('selectCouponobj', {})
				this.couponobj = this.$store.state.couponobj
				this.computedTotalPrice()
			},
			
			//详情
			Onorderdetails() {
				let url = ''
				if (this.type == 'tw') {
					url = 'detailsConsultationOrder'
				}else if (this.type == 'sp') {
					url = 'detailsConsultationVideoOrder'
				}else if (this.type == 'report') {
					url = 'detailsConsultationReportOrder'
				}else if (this.type == 'qy') {
					url = 'detailsConsultationSignOrder'
				}else{
					uni.showToast({
						title: '参数错误',
						icon: 'none',
						duration: 2000
					})
					return false
				}

				this.$Http({
					url: url,
					data: {
						order_code: this.id,
					},
				}).then(res => {
					if (res.code = 1) {
						this.detailsobj = res.data
						var images = this.detailsobj.images
						if(images) images = (images.substring(images.length-1)==',')?images.substring(0,images.length-1):images;
						images = images?images.split(','):[]
						this.detailsobj.images = images
						console.log(this.detailsobj)
						
						this.computedTotalPrice()
					}
				})
			},
			//倒计时
			Oncountdown() {
				var time = 900;
				var timer = null
				var that = this
				timer = setInterval(function() {
					time = time - 1;
					var minute = parseInt(time / 60);
					var second = parseInt(time % 60);
					if (time == 0) {
						clearInterval(timer);
					}
					let countdownminute = minute < 10 ? '0' + minute : minute
					let countdownsecond = second < 10 ? '0' + second : second
					that.countdown = countdownminute + ':' + countdownsecond
				}, 1000);
			},
			//去往等待页面
			Onpaywait() {
				let that = this
				let url = ''
				if (this.type == 'tw') {
					url = 'payConsultationOrder'
				}else if (this.type == 'sp') {
					url = 'payConsultationVideoOrder'
				}else if (this.type == 'report') {
					url = 'payConsultationReportOrder'
				}else if (this.type == 'qy') {
					url = 'payConsultationSignOrder'
				}else{
					uni.showToast({
						title: '参数错误',
						icon: 'none',
						duration: 2000
					})
					return false
				}

				this.$Http({
					url: url,
					type:'POST',
					data: {
						order_code: this.id,
						coupon_id: this.couponobj?this.couponobj.id:0,
					},
				}).then(res => {
					if (res.code == 1) {
						if(res.msg == 'pay_success'){
							if (that.type == 'sp') {
								uni.redirectTo({
									url: '/pagesA/my/videoList/videoList?type=1',
								})
							}
							if (that.type == 'tw') {
								uni.redirectTo({
									url: '/pagesA/my/textList/textList?type=1',
								})
							}
							return
						}
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.timeStamp,
							nonceStr: res.data.nonceStr,
							package: res.data.pacKage,
							signType: res.data.signType,
							paySign: res.data.paySign,
							success: function(res1) {
								if (that.type == 'sp') {
									uni.redirectTo({
										url: '/pagesA/my/videoList/videoList?type=1',
									})
								}
								if (that.type == 'tw') {
									uni.redirectTo({
										url: '/pagesA/my/textList/textList?type=1',
									})
								}
								if (that.type == 'report') {
									uni.redirectTo({
										url: '/pagesL/my/reportpage?type=1',
									})
								}
							},
							fail: function(err) {
								if (that.type == 'sp') {
									uni.redirectTo({
										url: '/pagesA/my/videoList/videoList?type=2',
									})
								}
								if (that.type == 'tw') {
									uni.redirectTo({
										url: '/pagesA/my/textList/textList?type=2',
									})
								}
								if (that.type == 'report') {
									uni.redirectTo({
										url: '/pagesL/my/reportpage?type=2',
									})
								}
							}
						});
					}
				})

			},
			
			gotoSelectCoupon(){
				var price = parseFloat(this.consultation_money || 0);
				if(price<=0) return;
			
				uni.navigateTo({
					url: '/pagesL/my/couponSelect?type=1&price='+price
				})
			},
		}
	}
</script>

<style lang="less">
	@import url("style.less");
	page{background-color: #f6f6f6;}
	
	.coupon{
		border-top: 1px solid #E9E9E9;
		padding: 26rpx 40rpx;
	}
	.hjprice{
		border-top: 1px solid #E9E9E9;
		padding: 30rpx 48rpx;
		
		.price{
			font-size: 32rpx;
			color: #F48E17;
		}
	}
</style>
